#include <windows.h>
#include <stdio.h>
 
#define NOME_DO_SERVICO "Scrabble Server"
#define CAMINHO "c:\\TPSO2\\Scrabble_Server.exe"

void Unnistall()
{
	SC_HANDLE schSCManager,schService;
	schSCManager = OpenSCManager( 
		NULL,                    // local machine 
		NULL,                    // ServicesActive database 
		SC_MANAGER_ALL_ACCESS);  // full access rights 

	if (schSCManager == NULL) 
		printf("Erro: OpenSCManager\n"); 

	schService = OpenService( 
		schSCManager,       // SCManager database 
		NOME_DO_SERVICO,       // name of service 
		DELETE);            // only need DELETE access 

	if (schService == NULL) 
		printf("Error: OpenService\n"); 

	if (! DeleteService(schService) ) 
		printf("Error: DeleteService\n"); 
	else 
		printf("DeleteService SUCCESS\n"); 

	CloseServiceHandle(schService); 
	CloseServiceHandle(schSCManager); 
}

void Install()
{
	SC_HANDLE schSCManager,schService;
	schSCManager = OpenSCManager( 
		NULL,                    // local machine 
		NULL,                    // ServicesActive database 
		SC_MANAGER_ALL_ACCESS);  // full access rights 

	if (schSCManager == NULL) 
		printf("Erro: OpenSCManager\n"); 


	LPCTSTR lpszBinaryPathName = CAMINHO;

	schService = CreateService( 
		schSCManager,              // SCManager database 
		NOME_DO_SERVICO,           // name of service 
		"SO2 - "NOME_DO_SERVICO,           // service name to display 
		SERVICE_ALL_ACCESS,        // desired access 
		SERVICE_WIN32_OWN_PROCESS, // service type 
		SERVICE_DEMAND_START,      // start type 
		SERVICE_ERROR_NORMAL,      // error control type 
		lpszBinaryPathName,        // service's binary 
		NULL,                      // no load ordering group 
		NULL,                      // no tag identifier 
		NULL,                      // no dependencies 
		NULL,                      // LocalSystem account 
		NULL);                     // no password 

	if (schService == NULL) 
		printf("Error: CreateService\n"); 
	else 
		printf("CreateService SUCCESS.\n"); 

	CloseServiceHandle(schService); 
	CloseServiceHandle(schSCManager); 
}

void main(int argc, char* argv[])
{
	
	if( argc < 2){
		printf("Uso incorrect:\n\t-i para instalar\n\t-u para desinstalar");
		return;
	}


	switch( argv[1][1] )
	{

	case 'i':
		Install();
		break;
	case 'u':
		Unnistall();
		break;
	}
		getchar();
}